package com.hankol.community.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.activity.BaseFragment
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleUtils
import com.baselibrary.utils.NumberUtils
import com.baselibrary.utils.ToastUtils
import com.hankol.community.R
import com.hankol.community.adapter.PulpitHomeCatalogueAdapter
import com.hankol.community.api.CommunityConstant
import com.hankol.community.bean.PulCourseBean
import com.hankol.community.bean.PulpitInfoBean
import com.hankol.community.bean.PulpitListBean
import com.hankol.community.databinding.FragmentPulpitHomeInfoBinding
import com.hankol.community.viewmodel.PulpitViewModel

/**
 * @description 讲坛主页【详情】tab\目录
 * @author 关忆北、
 * @time 2022/4/26 9:52 上午
 */
class PulpitHomeInfoFragment() : BaseFragment() {
    private lateinit var mViewModel: PulpitViewModel
    var mAdapter: PulpitHomeCatalogueAdapter? = null
    private lateinit var mDataBinding: FragmentPulpitHomeInfoBinding
    private var infoBean: PulpitInfoBean? = null
    private var isPay: Int = 0
    private var lastVisiblePosition = 4
    private var totalCount = 1
    override fun init() {
        mDataBinding = getDataBinding<FragmentPulpitHomeInfoBinding>()
        mViewModel = getFragmentViewModelProvider(this).get(PulpitViewModel::class.java)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_pulpit_home_info
    }

    fun setPay(isPay: Int) {
        this.isPay = isPay

    }

    /**
     * 讲坛主页 tab
     */
    fun setHomeView(infoBean: PulpitInfoBean) {
        //详情介绍
        this.infoBean = infoBean
        mDataBinding.tvPulpitHomeInfo.text = infoBean.desc
        mDataBinding.tvPulpitHomeInfo.visibility = View.VISIBLE
        mDataBinding.nesPulpitHomeInfo.visibility = View.VISIBLE
        mDataBinding.rvPulpitHomeCatalogue.visibility = View.GONE
    }

    /**
     * 讲坛主页 视频列表
     */
    fun setHomeVideoListView(pulpitId: Long, infoListBean: List<PulpitListBean>, totalCount: Int) {
        //目录列表
        this.totalCount = totalCount
        mAdapter = PulpitHomeCatalogueAdapter()
        mDataBinding.tvPulpitHomeInfo.visibility = View.GONE
        mDataBinding.nesPulpitHomeInfo.visibility = View.GONE
        mDataBinding.rvPulpitHomeCatalogue.visibility = View.VISIBLE
        mDataBinding.rvPulpitHomeCatalogue.adapter = mAdapter
        val l: LinearLayoutManager = mDataBinding.rvPulpitHomeCatalogue.layoutManager as LinearLayoutManager
        mDataBinding.rvPulpitHomeCatalogue.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                lastVisiblePosition = l.findLastVisibleItemPosition()
            }
        })
        mAdapter!!.isLock = isPay == 1
        mAdapter!!.setNewData(infoListBean)
        mAdapter!!.setOnItemClickListener { _, _, position ->
            if (DoubleUtils.isFastDoubleClick()) return@setOnItemClickListener
            if (isPay == 1) {
                val hashMap: HashMap<String, Any> = hashMapOf()
                hashMap[CommunityConstant.PUL_COURSE_ID] = mAdapter!!.data[position].id
                hashMap[CommunityConstant.PULPIT_ID] = pulpitId
                ARouterUtils.jump(ActivityRouter.COMMUNITY.COMMUNITY_PULPIT_VIDEO, hashMap)
            } else {
                ToastUtils.showShort(context, "解锁讲坛后才能观看")
            }
        }
    }

    /**
     * 讲坛视频页，tab
     */
    fun setVideoInfoView(homeTabType: Int, infoBean: PulCourseBean) {
        if (homeTabType == CommunityConstant.PULPIT_HOME_INFO) {
            //详情介绍
            mDataBinding.tvPulpitHomeInfo.text = infoBean.desc
            mDataBinding.tvPulpitHomeInfo.visibility = View.VISIBLE
            mDataBinding.rvPulpitHomeCatalogue.visibility = View.GONE
        }
    }

    fun getProgress(): Double {
        return if (mDataBinding.rvPulpitHomeCatalogue.canScrollVertically(1)) {
            NumberUtils.getDouble(lastVisiblePosition, totalCount)
        } else {
            100.00
        }
    }
}